// search模块的小仓库
import { reqGetSearchInfo } from '@/api'

const state = {
    searchInfo:{}
}
const actions = {
    async getSearchInfo({commit}, params = {}) {
        //当前这个 reqGetSearchInfo 函数在调用时至少传递一个参数（空对象）
        //params 形参：是当用户派发 action 的时候，第二个参数传递过来的，至少是一个对象
        let res = await reqGetSearchInfo(params)
        // console.log(res);
        if(res.code == 200) {
            commit('GETSEARCHINFO', res.data)
        }
    }
}
const mutations = {
    GETSEARCHINFO(state, searchInfo) {
        state.searchInfo = searchInfo
    }
}
const getters = {
    goodsList(state){
        //如果没有网络或网络慢应该返回一个空数组
        return state.searchInfo.goodsList || []
    },
    trademarkList(state){
        return state.searchInfo.trademarkList
    },
    attrsList(state) {
        return state.searchInfo.attrsList
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}